package com.estore.model;
import java.util.*;

/**
 * 商品实体类
 */
public class Product implements Comparable<Product> {

    private final String id;           // 商品ID - 设为final确保不可变
    private String name;               // 商品名称
    private double price;              // 价格
    private int stock;                 // 库存
    private Set<String> tags;          // 商品标签集合

    /**
     * 构造方法1：不传入标签，初始化为空的HashSet
     */
    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, new HashSet<>());
    }

    /**
     * 构造方法2：传入标签集合
     */
    public Product(String id, String name, double price, int stock, Set<String> tags) {
        this.id = Objects.requireNonNull(id, "商品ID不能为null");
        this.name = Objects.requireNonNull(name, "商品名称不能为null");

        if (price < 0) throw new IllegalArgumentException("价格不能为负数");
        if (stock < 0) throw new IllegalArgumentException("库存不能为负数");

        this.price = price;
        this.stock = stock;
        // 如果传入的tags为null，则初始化为空的HashSet，否则使用传入的集合
        this.tags = tags != null ? new HashSet<>(tags) : new HashSet<>();
    }

    // Getters
    public String getId() { return id; }
    public String getName() { return name; }
    public double getPrice() { return price; }
    public int getStock() { return stock; }

    /**
     * tags的getter方法
     * 返回不可修改的集合视图，防止外部直接修改内部集合
     */
    public Set<String> getTags() {
        return Collections.unmodifiableSet(tags);
    }

    // Setters
    public void setName(String name) {
        this.name = Objects.requireNonNull(name, "商品名称不能为null");
    }

    public void setPrice(double price) {
        if (price < 0) throw new IllegalArgumentException("价格不能为负数");
        this.price = price;
    }

    public void setStock(int stock) {
        if (stock < 0) throw new IllegalArgumentException("库存不能为负数");
        this.stock = stock;
    }

    /**
     * tags的setter方法
     * 接收一个Set，创建新的HashSet来避免外部修改影响内部数据
     */
    public void setTags(Set<String> tags) {
        this.tags = tags != null ? new HashSet<>(tags) : new HashSet<>();
    }

    // 业务方法
    public void reduceStock(int quantity) {
        if (quantity <= 0) throw new IllegalArgumentException("减少数量必须大于0");
        if (quantity > stock) throw new IllegalStateException("库存不足");
        this.stock -= quantity;
    }

    public void increaseStock(int quantity) {
        if (quantity <= 0) throw new IllegalArgumentException("增加数量必须大于0");
        this.stock += quantity;
    }

    public boolean isInStock() {
        return stock > 0;
    }

    public boolean hasSufficientStock(int requiredQuantity) {
        return requiredQuantity > 0 && stock >= requiredQuantity;
    }

    /**
     * 修改后的toString方法，包含标签信息
     */
    @Override
    public String toString() {
        String baseInfo = String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d",
                id, name, price, stock);

        // 如果有标签，则在末尾显示标签信息
        if (tags != null && !tags.isEmpty()) {
            return baseInfo + " | 标签: " + tags;
        } else {
            return baseInfo;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Product other) {
        int priceCompare = Double.compare(this.price, other.price);
        return priceCompare != 0 ? priceCompare : this.name.compareTo(other.name);
    }
}